package com.right.health.app.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.common.SendAuthCodeCommon;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.net.CertificationApi;
import com.right.health.app.utils.EnterVerifyUtil;

/*
 * Created by hopeliao on 2019-09-27
 * 医院预留信息认证
 *
 */
public class CertificationByHospitalActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private static final int HTTP_CODE_GET_CODE = 1;
    private static final int HTTP_CODE_SAVE = 2;

    private EditText mPhoneEt, mCodeEt, mNameEt, mIdcardEt;

    private TextView mVerificationTv;

    private SendAuthCodeCommon mSendAuthCodeCommon;

    private CertificationApi mCertificationApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.certification_by_hospital);
    }

    @Override
    public void findView() {
        mPhoneEt = (EditText) findViewById(R.id.et_phone);
        mCodeEt = (EditText)findViewById(R.id.et_code);
        mNameEt = (EditText) findViewById(R.id.et_name);
        mIdcardEt = (EditText) findViewById(R.id.et_idcard);

        mVerificationTv = (TextView) findViewById(R.id.btn_get_verification);
        mVerificationTv.setOnClickListener(this);

        findViewById(R.id.tv_submit).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mCertificationApi = new CertificationApi();
        mCertificationApi.setListener(this);

        mSendAuthCodeCommon = new SendAuthCodeCommon(milliseconds -> {
            mVerificationTv.setText(milliseconds == 0 ? getString(R.string.again_send_code) :
                    String.format(getString(R.string.format_seconds),
                            String.valueOf(milliseconds / 1000)));

            if (milliseconds == 0) {
                mVerificationTv.setClickable(true);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_submit:
                send2Net();
                break;
            case R.id.btn_get_verification:
                mVerificationTv.setClickable(false);
                String phone = mPhoneEt.getText().toString().trim();
                if (!VerifyUtil.isMobile(phone)) {
                    mVerificationTv.setClickable(true);
                    ToastUtil.show(App.getInstance(), R.string.please_enter_phone);
                    return;
                }
                mCertificationApi.addRequestCode(HTTP_CODE_GET_CODE);
                mCertificationApi.getVerificationCode(phone);
                break;
        }
    }

    private void send2Net() {
        String phone = mPhoneEt.getText().toString().trim();
        if (!VerifyUtil.isMobile(phone)) {
            ToastUtil.show(App.getInstance(), R.string.please_enter_phone);
            return;
        }
        String code = mCodeEt.getText().toString().trim();
        if(TextUtils.isEmpty(code)) {
            ToastUtil.show(App.getInstance(),R.string.please_enter_code);
            return;
        }
        String name = mNameEt.getText().toString().trim();
        if(TextUtils.isEmpty(name)) {
            ToastUtil.show(App.getInstance(),mNameEt.getHint().toString().trim());
            return;
        }
        String idcard = mIdcardEt.getText().toString().trim();
        if(TextUtils.isEmpty(idcard) || !EnterVerifyUtil.verifyCardid(idcard)) {
            ToastUtil.show(this, R.string.please_enter_id_card_number);
            return;
        }

        mCertificationApi.addRequestCode(HTTP_CODE_SAVE);
        mCertificationApi.certificationByHospital(phone, code, idcard, name, App.getInstance().getString(R.string.def_hospital_code));
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            if(request.requestCode == HTTP_CODE_GET_CODE) {
                mVerificationTv.setClickable(true);
            }
            ToastUtil.show(this, response.error);
        } else {
            if(request.requestCode == HTTP_CODE_GET_CODE) {
                ToastUtil.show(App.getInstance(), "验证码发送成功");
                mSendAuthCodeCommon.startCountdown();
            } else if(request.requestCode == HTTP_CODE_SAVE) {
                ToastUtil.show(this, "提交认证成功");

                AppUserResponse.AppUserInfo userInfo = App.getInstance().getUser();
                userInfo.verifystatus = "1";
                App.getInstance().refreshUser(userInfo);

                sendBroadcast(BroadCastCode.REFRESH_USER);

                finish();
            }
        }
    }
}
